package com.danieltoms.gcSync.addressbook.widgets;

import net.rim.device.api.ui.ContextMenu;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.ObjectChoiceField;

import com.danieltoms.gcSync.addressbook.screens.EditGmailInfo;

public class MoveableEditFieldMenu
{
	int _fieldStart = 0;
	EditField _field = null;

	public MoveableEditFieldMenu(EditField field, int start)
	{
		_field = field;
		_fieldStart = start;

	}

	private MenuItem moveUpMenuItem = new MenuItem("Move Up", 100, 10)
	{
		public void run()
		{
			moveUp();
		}
	};
	private MenuItem moveDownMenuItem = new MenuItem("Move Down", 200, 10)
	{
		public void run()
		{
			moveDown();
		}
	};
	private MenuItem deleteEmailMenuItem = new MenuItem("Delete Email", 300, 10)
	{
		public void run()
		{
			delete();
		}
	};

	private void moveUp()
	{
		int move = _myIndex;
		move -= 2;

		EditField me = (EditField) _myManager.getField(_myIndex);
		ObjectChoiceField type = (ObjectChoiceField) _myManager.getField(_myIndex + 1);

		_myManager.delete(me);
		_myManager.delete(type);

		if (move == _fieldStart)
		{
			// if we moved into the primary position, swap the object array
			ObjectChoiceField primary = (ObjectChoiceField) _myManager.getField(move + 1);
			_myManager.delete(primary);

			_myManager.insert(me, move);
			_myManager.insert(primary, move + 1);
			_myManager.insert(type, move + 3);
		}
		else
		{
			_myManager.insert(me, move);
			_myManager.insert(type, move + 1);
		}
	}

	protected void delete()
	{
		Field email = _myManager.getField(_myIndex);
		Field type = _myManager.getField(_myIndex + 1);
		_myManager.delete(email);
		_myManager.delete(type);

		_myManager.deleteEditField((MoveableEditFieldType) _field);

		_myManager.setDirty(true);

	}

	private void moveDown()
	{
		int move = _myIndex;

		move += 2;
		// ClickableEmailAddressEditField me = (ClickableEmailAddressEditField)
		// _myManager.getField(_myIndex);
		ObjectChoiceField type = null;
		_myManager.delete(_field);

		if ((move - 2 == _fieldStart) && _field instanceof MoveableEmailAddressEditField)
		{
			// if we are moving the primary down
			// type = primary
			EditField swap = (EditField) _myManager.getField(move - 1);

			_myManager.delete(swap);

			_myManager.insert(swap, move - 2);
			_myManager.insert(_field, move);
		}
		else
		{
			type = (ObjectChoiceField) _myManager.getField(_myIndex);
			System.out.println(type.getChoice(type.getSelectedIndex()));
			_myManager.delete(type);
			_myManager.insert(_field, move);
			_myManager.insert(type, move + 1);
		}

	}

	EditGmailInfo _myManager = null;
	int _myIndex = 0;

	protected ContextMenu makeContextMenu(ContextMenu contextMenu)
	{

		_myManager = (EditGmailInfo) _field.getScreen();
		_myIndex = _field.getIndex();

		if (_field.getIndex() - 2 > 0)
			if (_field.getManager().getField(_field.getIndex() - 2) instanceof EditField)
				contextMenu.addItem(moveUpMenuItem);

		if (_field.getIndex() + 2 < _field.getManager().getFieldCount())
			if (_field.getManager().getField(_field.getIndex() + 2) instanceof EditField)
			{
				contextMenu.addItem(moveDownMenuItem);
			}

		contextMenu.addItem(deleteEmailMenuItem);
		return contextMenu;
	}

}
